package com.yangtu.nearbyshop.core.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.math.BigDecimal;

public class GeoUtils {
    private static final Log logger = LogFactory.getLog(GeoUtils.class);
    private static final double EARTH_RADIUS = 6378.137;
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }

    public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
    {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
                Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }
    public static double D_jw(double wd1,double jd1,double wd2,double jd2)
    {
        double x,y,out;
        double PI=3.14159265;
        double R=6.371229*1e6;

        x=(jd2-jd1)*PI*R*Math.cos( ((wd1+wd2)/2) *PI/180)/180;
        y=(wd2-wd1)*PI*R/180;
        out=Math.hypot(x,y);
        return out/1000;
    }

    public static BigDecimal calcBucha(double fromLon, double fromLat, double toLon, double toLat){
        logger.info("jisuan dis= " + fromLon + "," + fromLat + "---" + toLon + "," + toLat);
        BigDecimal bucha = new BigDecimal("0.0");
        double miles = GeoUtils.D_jw(fromLat,fromLon,toLat,toLon);
        logger.info("miles = " + miles);
        if(miles>1000 && miles< 1500){
            bucha = bucha.add(new BigDecimal("2"));
        }else if(miles>=1500 ) {
            bucha = new BigDecimal(miles/500).setScale( 0, BigDecimal.ROUND_DOWN );
        }else {
            bucha = new BigDecimal("0.0");
        }
        return bucha;
    }

    public static void main(String[] args) {
        //System.out.println(calcBucha(116.41637,25.92855,117.183739,39.068493).toPlainString()); ;

//        BigDecimal bucha = new BigDecimal(1549.32/500).setScale( 0, BigDecimal.ROUND_DOWN );
//        System.out.println(bucha);

        BigDecimal act = new BigDecimal(0.13);
        Integer refundFee = act.multiply(new BigDecimal(100)).intValue();
        System.out.println(refundFee);
    }
}
